import {
  Controller,
  Post,
  Body,
  Get,
  Param,
  Put,
  Delete,
  UseGuards,
  Request,
  NotFoundException,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { CreateUserDto } from './create-user.dto'; // 引入 DTO
import { UpdateUserDto } from './update-user.dto';
import { AuthGuard } from '@nestjs/passport'; // 使用 passport 的 jwt 认证守卫
import { JwtPayload } from '../auth/jwt-payload.interface'; // 根据你的项目结构引入
import { RoleGuard } from 'src/role/role.guard';
import { ApiResponse } from 'src/common/reponse.interface'; // 引入统一响应格式
@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Post('create')
  async create(
    @Body() createUserDto: CreateUserDto,
  ): Promise<ApiResponse<any>> {
    // 修改为统一响应格式
    const result = await this.usersService.create(createUserDto);
    return {
      statusCode: 201,
      message: 'User created successfully',
      data: result,
    };
  }

  @UseGuards(AuthGuard('jwt'), RoleGuard) // 使用 JWT 认证
  @Get()
  async findAll(): Promise<ApiResponse<any>> {
    return this.usersService.findAll();
  }

  @UseGuards(AuthGuard('jwt')) // 使用 JWT 认证
  @Get('me')
  async getMe(@Request() req): Promise<ApiResponse<any>> {
    const user = req.user; // 从 JWT 中提取用户信息
    return {
      statusCode: 200,
      message: '获取用户信息成功',
      data: user,
    };
  }

  // 更新用户信息
  @UseGuards(AuthGuard('jwt'), RoleGuard) // 使用 JWT 认证
  @Put(':usernameOrId')
  async update(
    @Param('usernameOrId') usernameOrId: string,
    @Body() updataUserDto: UpdateUserDto, // 更新时也使用 DTO
  ): Promise<ApiResponse<any>> {
    const idOrUsername = isNaN(Number(usernameOrId))
      ? usernameOrId
      : Number(usernameOrId);
    return this.usersService.update(
      idOrUsername,
      updataUserDto.oldPassword,
      updataUserDto.newPassword,
      updataUserDto.roleId,
    );
  }

  // 删除用户
  @UseGuards(AuthGuard('jwt'), RoleGuard) // 使用 JWT 认证
  @Delete(':usernameOrId')
  async delete(
    @Param('usernameOrId') usernameOrId: string,
  ): Promise<ApiResponse<any>> {
    const idOrUsername = isNaN(Number(usernameOrId))
      ? usernameOrId
      : Number(usernameOrId);
    return this.usersService.delete(idOrUsername);
  }

  // 获取当前用户的菜单
  @UseGuards(AuthGuard('jwt')) // 使用 JWT 认证
  @Get('menu') // 路由不需要用户ID，直接获取当前用户菜单
  async getUserMenu(@Request() request: any): Promise<ApiResponse<any>> {
    const user = request.user as JwtPayload; // 或使用你自己的 User 类型

    if (!user || !user.sub) {
      throw new NotFoundException('用户信息未找到');
    }
    const menu = await this.usersService.getUserMenu(user.sub);
    return {
      statusCode: 200,
      message: '获取信息成功',
      data: menu,
    }; // 获取当前用户的菜单
  }

  @UseGuards(AuthGuard('jwt')) // 使用 JWT 认证
  @Get('/info')
  async getInfo(@Request() req): Promise<ApiResponse<any>> {
    const userId = req.user.data.id; // 从 JWT 中提取用户信息
    const result = await this.usersService.findOne(userId);
    if (!result) {
      throw new NotFoundException('用户信息未找到');
    }
    return {
      statusCode: 200,
      message: '获取信息成功',
      data: result,
    };
  }
}
